﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor.VersionControl;
using static UnityEngine.LightProbeProxyVolume;

/// <summary>
/// 管理mod是否启用。
/// </summary>
public static class ModsConfig
{

    public class ModsConfigData//这个也是存的List
    {
        public string version;
        public List<string> activeMods = new List<string>();
    }
    private static ModsConfigData modsConfigData;
    /// <summary>
    /// 快速检索mod设备
    /// </summary>
    private static HashSet<string> activeModsHashSet = new HashSet<string>();

    static ModsConfig()
    {

    }


    /// <summary>
    /// 启用的mod，现在是读入就等于启用
    /// </summary>
    static List<ModMetaData> activeModsInLoadOrder = new List<ModMetaData>();
    static List<ModMetaData> noActiveModsInLoadOrder = new List<ModMetaData>();


    public static List<ModMetaData> ActiveModsInLoadOrder => activeModsInLoadOrder;

    static public void SaveChanageActiveMod(IEnumerable<string> activeMods)
    {
        modsConfigData.activeMods.Clear();
        foreach (var mod in activeMods)
        {
            modsConfigData.activeMods.Add(mod);
        }
        RefreshMods();
    }

    static public void RefreshMods()
    {
        activeModsInLoadOrder.Clear();
        noActiveModsInLoadOrder.Clear();
        

        for (int i = 0; i < ModLister.mods.Count; i++)
        {

            ModMetaData modMetaData = ModLister.mods[i];


            if (modsConfigData.activeMods.Contains(modMetaData.PackageId))
            {
                activeModsHashSet.Add(modMetaData.PackageId);
                activeModsInLoadOrder.Add(modMetaData);
            }
            else
            {
                noActiveModsInLoadOrder.Add(modMetaData);
            }
            //ModsConfig.ActiveModsInLoadOrder[i].FolderName;
        }
    }


    static public void GetMods(out List<ModMetaData> active, out List<ModMetaData> noActive)
    {
        RefreshMods();
        active = activeModsInLoadOrder;
        noActive = noActiveModsInLoadOrder;

    }



    /// <summary>
    /// 如果要做Mod拆分，要基于存档来绑定Mod载入数据，所以得用string
    /// 用UITookit写一个runtime时的Mod次序
    /// 直接存HashSet为唯一的mod路径
    /// hashSet不能整读取排序
    /// </summary>

    public static void LoadArchiveMod(ModsConfigData activeModsHashSet)
    {
        ModsConfig.modsConfigData = activeModsHashSet;
    }




    public static void Reset()
    {



    }

    public static bool AreAllActive(string mods)
    {
        if (mods != null && mods.Contains(','))
        {
            string[] array = mods.ToLower().Split(',');
            for (int i = 0; i < array.Length; i++)
            {
                if (!IsActive(array[i].Trim()))
                {
                    return false;
                }
            }
            return true;
        }
        return IsActive(mods);
    }

    public static bool IsActive(string id)
    {
        return activeModsHashSet.Contains(id.ToLower());
    }
}

